#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib.parse
import urllib.request
import json

def test_datamanage_api_acquisitionrecords():
    req = urllib.request.Request(url='http://127.0.0.1:5000/api/system_initialization', method='PUT')
    with urllib.request.urlopen(req) as f:
        print("status: /api/system_initialization")
        print(f.status)
    
    ## query 1
    headers = {'Content-Type': 'application/json'}
    data = {"countperpage": 5, 
            "experimentdate": "", 
            "pageindex": 0, 
            "person_name": "",
            "subject_name": ""}
    data = bytes(json.dumps(data), encoding='utf8')

    req = urllib.request.Request(url='http://127.0.0.1:5000/api/acquisitionrecords', headers=headers, data=data, method='POST')
    with urllib.request.urlopen(req) as f:
        print(f"status: /api/acquisitionrecords, query:{0}", data)
        print(f.status)
        print(f.read())

    ## query 2
    headers = {'Content-Type': 'application/json'}
    data = {"countperpage": 5, 
            "experimentdate": "", 
            "pageindex": 2, 
            "person_name": "",
            "subject_name": ""}
    data = bytes(json.dumps(data), encoding='utf8')

    req = urllib.request.Request(url='http://127.0.0.1:5000/api/acquisitionrecords', headers=headers, data=data, method='POST')
    with urllib.request.urlopen(req) as f:
        print(f"status: /api/acquisitionrecords, query:{0}", data)
        print(f.status)
        print(f.read())

    ## query 3
    headers = {'Content-Type': 'application/json'}
    data = {"countperpage": 100, 
            "experimentdate": "", 
            "pageindex": 0, 
            "person_name": "",
            "subject_name": ""}
    data = bytes(json.dumps(data), encoding='utf8')

    req = urllib.request.Request(url='http://127.0.0.1:5000/api/acquisitionrecords', headers=headers, data=data, method='POST')
    with urllib.request.urlopen(req) as f:
        print(f"status: /api/acquisitionrecords, query:{0}", data)
        print(f.status)
        print(f.read())

    ## query 4
    headers = {'Content-Type': 'application/json'}
    data = {"countperpage": 5, 
            "experimentdate": "", 
            "pageindex": 100, 
            "person_name": "",
            "subject_name": ""}
    data = bytes(json.dumps(data), encoding='utf8')

    req = urllib.request.Request(url='http://127.0.0.1:5000/api/acquisitionrecords', headers=headers, data=data, method='POST')
    with urllib.request.urlopen(req) as f:
        print(f"status: /api/acquisitionrecords, query:{0}", data)
        print(f.status)
        print(f.read())

    ## query 5
    headers = {'Content-Type': 'application/json'}
    data = {"countperpage": 5, 
            "experimentdate": "", 
            "pageindex": 0, 
            "person_name": "",
            "subject_name": "牛"}
    data = bytes(json.dumps(data), encoding='utf8')

    req = urllib.request.Request(url='http://127.0.0.1:5000/api/acquisitionrecords', headers=headers, data=data, method='POST')
    with urllib.request.urlopen(req) as f:
        print(f"status: /api/acquisitionrecords, query:{0}", data)
        print(f.status)
        print(f.read())


if __name__ == '__main__': 
    print("API Test of PAI datamage API.")
    test_datamanage_api_acquisitionrecords()